home *** CD-ROM | disk | FTP | other *** search
- Path: news.sprintlink.net!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Class Engineering Issues
- Date: Tue, 26 Mar 1996 12:09:31 -0500
- Organization: Datalytics, Inc
- Message-ID: <315824CB.3F94@datalytics.com>
- References: <3156ecf9.611110371@sun.cis.smu.edu>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Damon Bowman wrote:
- >
- > I am learning C++, and I think I need some additional rules of thumb
- > to use when constructing classes.
- >
- > What are some general rules of thumb to use when deciding whether or
- > not a particular function should be a class member or not?
- > Simply put, there aren't any. The concept of a class dicates
- what should be in it. If you have a class describing a certain
- thing, then all information and functions for manipulating that
- information should be part of that class. (There are exceptions
- required for things like commutivity.) Once you've identified
- the classes required to describe the problem, you need to
- determine what they must do and know.
-
- I have seen people create a class for the sole purpose of
- collecting functions in one place. They apparently think that
- C++ requires everything to be in a class. If the function
- doesn't apply to a class, don't force it. If it applies to more
- than one class, perhaps it should be a base class member
- function.
-
- > What are some rules for deciding if a function should be public or
- > private?
- > You must determine "who" needs access to it. If it provides
- only implementation that no one, not even derived classes,
- should use, it should be private. If it is fundamental to the
- use of the class it should, possibly, be public. The
- distinction between protected and public access is gray. You
- have to decide whether the function is one you want to make
- available always to users of your class or those derived from
- it. If you think it should be restricted--perhaps due to the
- damage it can do--you might make it protected and require a
- derived class to expose it should that become necessary.
- Other than that, you're restricting use to classes derived from
- yours, saying, in effect, I'm restricting access purposely
- because I don't think the general class user should invoke this
- function.
-
- > Should argument checking ALWAYS be done by the member functions? What
- > criteria are used to decide this?
-
- I frequently code debug version argument checking using
- assertions. For things that can be devastating, like a null
- pointer dereference, I code a runtime validation. The former
- helps the developer (even you) use the function correctly before
- the code goes production. The latter generates robust code.
-
- Did I answer your questions sufficiently?
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-